(function () {
	var ns = ij.pkg('core').namespaceString;
	ij.core.AnimatedObject = ij.core.DisplayObject.extend({
		init: function (config) {
			return this._super(ij.apply({
				classname: ns+'.AnimatedObject',
				classes: ['ij-object', 'ij-animated-object'],
				animations: {},
				currentAnimation: null
			}, config));
		},
		apply: function (extension, overwrite_existing) {
			var animations = {},
			config = ij.clone(extension);
			if (config && config.animations) {
				animations = config.animations;
				if (this.animations) {
					delete config.animations;
				} else {
					config.animations = {};
				}
			}
			
			this._super(config, overwrite_existing);
			
			ij.each(animations, function (i, item) {
				this.addAnimation(item);
			}, this);
		},
		addAnimation: function (animation) {
			if (!(animation instanceof ij.core.Animation)) {
				animation = new ij.core.Animation(ij.apply(ij.clone(animation), {
					animatedObject: this
				}));
			}
			this.animations[animation.name] = animation;
			
			return this;
		},
		startAnimation: function (animation_name) {
			if (this.currentAnimation && this.currentAnimation != animation_name) {
				this.currentAnimation.stop();
				this.currentAnimation = null;
			}
			if (this.animations[animation_name] && !this.animations[animation_name].running) {
				this.currentAnimation = this.animations[animation_name].start();
			}
		},
		stopAnimation: function (animation_name) {
			if (this.animations[animation_name] && this.animations[animation_name].running) {
				this.animations[animation_name].stop();
			}
		},
		render: function () {
			if (this.currentAnimation) {
				this.currentAnimation.step();
			}
			
			this._super();
		}
	});
})();
